package com.taoyibao.hongcanting.ui.home.views;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.BaseRelativeLayout;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.model.DishModel;
import com.taoyibao.hongcanting.ui.home.activity.MenuActivity;
import com.taoyibao.hongcanting.ui.login.activity.LoginActivity;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TangAnna
 * @description: 首页上半部分的镇店之宝
 * @date :${DATA} 14:04
 */
public class RecommendArea extends BaseRelativeLayout<List<DishModel>> {
    private RecyclerView mRecyclerView;
    private CommonAdapter mAdapter;
    private List<DishModel> mData;

    public RecommendArea(Context context) {
        super(context);
    }

    public RecommendArea(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RecommendArea(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        mData = new ArrayList<>();
        mRecyclerView = findViewById(R.id.rv_area_recommend_list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        mRecyclerView.setAdapter(mAdapter = new CommonAdapter<DishModel>(getContext(), R.layout.item_recommend_shell, mData) {
            @Override
            protected void convert(ViewHolder viewHolder, DishModel dishModel, int position) {
                ItemRecommendArea itemRecommendArea = (ItemRecommendArea) viewHolder.getConvertView();
                itemRecommendArea.setData(dishModel);
            }
        });
        //Item点击事件 默认事件是跳转页面
        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (mInterface != null) {
                    mInterface.onItemClick(mData.get(position));
                }
                if (SpUtils.getLoginStatus()) {
                    MenuActivity.open(getContext(), mData.get(position));
                } else {
                    LoginActivity.open(getContext(), "");
                }

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    public void setData(final List<DishModel> data) {
        if (data == null || data.size() < 1) {
            return;
        }
        mData.clear();
        mData.addAll(data);
        mAdapter.notifyDataSetChanged();

    }

    @Override
    protected int bindLayout() {
        return R.layout.area_recommend;
    }

    private RecommendAreaInterface mInterface;

    /**
     * 设置item的点击事件
     *
     * @param anInterface
     */
    public void setInterface(RecommendAreaInterface anInterface) {
        mInterface = anInterface;
    }

    public interface RecommendAreaInterface {
        void onItemClick(DishModel dishModel);
    }


}
